package top.deepdesigner.controller.market;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.deepdesigner.annotation.RequiresPermissionsDesc;
import top.deepdesigner.pojo.DtsIssue;
import top.deepdesigner.service.DtsIssueService;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.CommonListVO;

import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/22 星期四 22:33:31 <br/>
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/issue")
public class IssueController {

  @Autowired
  private DtsIssueService issueService;

  @RequiresPermissionsDesc(menu = {"商场管理", "通用问题"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:issue:list"})
  @GetMapping("/list")
  public Object list(@RequestParam(defaultValue = "1") Integer page,
                     @RequestParam(defaultValue = "20") Integer limit,
                     @RequestParam(defaultValue = "add_time") String sort,
                     @RequestParam(defaultValue = "desc") String order,
                     @RequestParam(defaultValue = "") String question) {

    CommonListVO<DtsIssue> issueListVO = new CommonListVO<>();
    //获取分页数量
    Long count = issueService.getIssueCount();
    List<DtsIssue> dtsIssues = issueService.getIssueList(page, limit, sort, order, question);
    issueListVO.setTotal(count);
    issueListVO.setItems(dtsIssues);
    return ResponseUtil.ok(issueListVO);
  }

  @RequiresPermissionsDesc(menu = {"商场管理", "通用问题"}, button = "添加")
  @RequiresPermissions(value = {"admin:issue:create"})
  @PostMapping("/create")
  public Object create(@RequestBody DtsIssue issue) {
    DtsIssue dtsIssue = issueService.createIssue(issue);
    if (dtsIssue == null) return ResponseUtil.fail(700, "关键词已存在");
    return ResponseUtil.ok(dtsIssue);
  }

  @RequiresPermissionsDesc(menu = {"商场管理", "通用问题"}, button = "修改")
  @RequiresPermissions(value = {"admin:issue:update"})
  @PostMapping("/update")
  public Object update(@RequestBody DtsIssue issue) {
    DtsIssue dtsIssue = issueService.updateIssue(issue);
    return ResponseUtil.ok(dtsIssue);
  }

  @RequiresPermissionsDesc(menu = {"商场管理", "通用问题"}, button = "删除")
  @RequiresPermissions(value = {"admin:issue:delete"})
  @PostMapping("/delete")
  public Object delete(@RequestBody DtsIssue issue) {
    issueService.deleteIssue(issue);
    return ResponseUtil.ok();
  }
}
